<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <div>
            <h2>小韩记事本</h2>
            <table>
                <input type="text" v-model="task" id="input" @keyup.enter="fun(task)">
                <button @click="fun(task)" id="btn">添加任务</button>
                <div v-for="(item,index) in List" :key="item.id">{{ index+1 }}. {{ item.name }}
                    <hr>
                </div>
                <div v-show="flag">合计：{{ List.length }} <span @click="clear()">清空任务</span></div>
            </table>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/vue@2.7.14/dist/vue.js"></script>

    <script>
        const app = new Vue({
            el: "#app",
            data: {
                List: [],
                task: '',
                flag: false
            },
            methods: {
                fun(add) {
                    jsonObject = {}
                    if (this.task === '') {
                        alert("请输入输入任务名称")
                        return
                    }

                    // 时间戳
                    jsonObject.id = +new Date()
                    jsonObject.name = this.task
                    // push是尾插 unshift是头插
                    this.List.unshift(jsonObject)
                    // this.List.push(jsonObject)
                    this.flag = true
                    this.task = ''
                },
                clear() {
                    this.List = []
                    this.flag = false
                }
            }
        })
    </script>
</body>

</html>